using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using RandomSearch.Common;

namespace RandomSearch.VM
{
    public class OptionExtractor
    {
        private static readonly Dictionary<Type, Func<DependencyObject, string>> Handlers;

        static OptionExtractor()
        {
            Handlers = new Dictionary<Type, Func<DependencyObject, string>>();

            Handlers.Add(typeof(TextBox), x =>
                                               {
                                                   var z = (TextBox)x;
                                                   return z.Text;
                                               });

            Handlers.Add(typeof(CheckBox), x =>
                                                {
                                                    var z = (CheckBox)x;
                                                    return z.IsChecked.ToString();
                                                });

            Handlers.Add(typeof(Calendar), x =>
                                                {
                                                    var z = (Calendar)x;
                                                    return z.SelectedDate.HasValue
                                                               ? z.SelectedDate.Value.ToString()
                                                               : null;
                                                });

            Handlers.Add(typeof(DatePicker), x =>
                                                  {
                                                      var z = (DatePicker)x;
                                                      return z.SelectedDate.HasValue
                                                                 ? z.SelectedDate.Value.ToString()
                                                                 : null;
                                                  });

            Handlers.Add(typeof(ComboBox), x =>
                                                {
                                                    var z = (ComboBox)x;
                                                    return z.SelectedValue as string;

                                                });
        }

        public static Dictionary<string, string> GetOptions(DependencyObject optionsContainer)
        {
            var options = new Dictionary<string, string>();

            foreach (Control c in optionsContainer.FindLogicalChildsByType<Control>())
            {
                Type t = c.GetType();
                if (Handlers.ContainsKey(t))
                {
                    string val = Handlers[t](c);
                    if (!string.IsNullOrWhiteSpace(val)) options.Add(c.Name, val);
                }
            }
            return options;
        }
    }
}